package com.itheima.mm.service.impl;

import com.itheima.mm.dao.CatalogDao;
import com.itheima.mm.dao.CourseDao;
import com.itheima.mm.dao.QuestionDao;
import com.itheima.mm.dao.UserDao;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.pojo.Catalog;
import com.itheima.mm.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CatalogDao catalogDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private CourseDao courseDao;
    @Override
    public List<Catalog> findListByPage(QueryPageBean queryPageBean) {
        List<Catalog> catalogList=catalogDao.findListByPage(queryPageBean);
        for (Catalog catalog : catalogList) {
            catalog = replenishCatalog(catalog);
        }
        return catalogList;
    }

    @Override
    public Long findTotal(QueryPageBean queryPageBean) {

        return catalogDao.findTotal(queryPageBean);
    }

    @Override
    public void add(Catalog catalog) {
        catalogDao.add(catalog);
    }

    @Override
    public void delete(Integer catalogId) {

        catalogDao.delete(catalogId);

    }

    private  Catalog replenishCatalog(Catalog catalog){
        catalog.setCreator(userDao.findById(catalog.getUserId()).getUsername());
        catalog.setQuestionQty(questionDao.findQuestionQtyByCatalogId(catalog.getId()));
        catalog.setCourse(courseDao.findCourseById(catalog.getCourseId()));
        catalog.setCourseName(catalog.getCourse().getName());
        return catalog;
    }
}
